extends Node

@export var look_speed: float = 0.5
@export var move_speed: float = 7.0
@export var camera: Node3D = null

var mouse_captured: bool = false

# 鼠标左右上下移动

func _unhandled_input(event: InputEvent) -> void:
	if !mouse_captured and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		mouse_captured = true
	elif mouse_captured and Input.is_key_pressed(KEY_ESCAPE):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		mouse_captured = false

	if mouse_captured and event is InputEventMouseMotion:
		rotate_look(event.relative)

# 也可使用rotate_x, rotate_y, rotate_z等方法
func rotate_look(relative: Vector2) -> void:
	# 上下旋转, 限制上下旋转的角度
	camera.rotation_degrees.x -= relative.y * look_speed
	camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, -45, 30)

	# 左右旋转，环绕
	camera.rotation_degrees.y -= relative.x * look_speed
	camera.rotation_degrees.y = wrapf(camera.rotation_degrees.y, 0, 360.0)
